﻿using Authority.DataTransferObject.ActionAuthority;
using Authority.IServices;
using Authority.IServices.Model.Action;
using Authority.PresentationModels.Action;
using AutoMapper;
using HelperFromTheSkeleton.APICore;
using HelperFromTheSkeleton.Model.PageModel;
using HelperFromTheSkeleton.Model.RequestModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace Authority.WebAPI.Controllers
{
    /// <summary>
    /// 功能权限控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ActionAuthorityController : BaseController
    {

        private readonly IActionAuthorityService _actionAuthorityService;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="actionAuthorityService"></param>
        /// <param name="mapper"></param>
        public ActionAuthorityController(IActionAuthorityService actionAuthorityService, IMapper mapper)
        {
            _actionAuthorityService = actionAuthorityService;
            _mapper = mapper;
        }

        /// <summary>
        /// 新增功能
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel> Add(AddActionRequestModel requestModel)
        {
            return await HandleAsync(async() =>
            {
                var model = _mapper.Map<AddActionModel>(requestModel);
                await _actionAuthorityService.AddAsync(model);
            });
        }

        /// <summary>
        /// 修改功能
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel> Edit(EditActionRequestModel requestModel)
        {
            return await HandleAsync(async () =>
            {
                var model = _mapper.Map<EditActionModel>(requestModel);
                await _actionAuthorityService.EditAsync(model);
            });
        }

        /// <summary>
        /// 删除功能
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel> Delete([Required]Guid id)
        {
            return await HandleAsync(async () =>
            {
                await _actionAuthorityService.DeleteAsync(id);
            });
        }

        /// <summary>
        /// 获取功能详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<List<ActionAuthoritytDTO>>> Get([Required] List<Guid> ids)
        {
            return await HandleAsync(async () =>
            {
                List<ActionAuthoritytDTO> result = await _actionAuthorityService.GetAsync(ids);
                return result;
            });
        }

        /// <summary>
        /// 获取功能详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResultModel<ActionAuthorityListDTO>> Query(QueryActionRequestModel requestModel)
        {
            return await HandleAsync(async () =>
            {
                var model = _mapper.Map<QueryActionModel>(requestModel);
                return await _actionAuthorityService.QueryAsync(model);
            });
        }

    }
}
